package com.baizhi.cmfzdemo.service.impl;

import com.baizhi.cmfzdemo.dao.CmfzArticleDao;
import com.baizhi.cmfzdemo.entity.CmfzArticle;
import com.baizhi.cmfzdemo.service.CmfzArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 54433 on 2019/4/4.
 */
@Service
public class CmfzArticleServiceImpl implements CmfzArticleService {
    //封装集合
    @Autowired
    private CmfzArticleDao cmfzArticleDao;
    @Override
    public Map queryAllCmfzArticle(int page, int rows) {
        //获取总数数量
        Map map = new HashMap();
        int count = cmfzArticleDao.getCount();
        int page2 = (page-1)*rows;
        try{
            List<CmfzArticle> cmfzArticles = cmfzArticleDao.queryAllCmfzArticle(page2, rows);
            map.put("total",count);
            map.put("rows",cmfzArticles);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map deleteCmfzArticle(int[] ids) {
        Map map = new HashMap();
        try{
            cmfzArticleDao.deleteCmfzArticle(ids);
            System.out.println("执行了删除操作**************");
            map.put("delete",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("delete",false);
        }
        return map;
    }

    @Override
    public Map addCmfzArticle(CmfzArticle cmfzArticle) {
        Map map = new HashMap();
        try {
            cmfzArticleDao.addCmfzArticle(cmfzArticle);
            map.put("add", true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            map.put("add", false);
        }
        return map;
    }

    @Override
    public Map updateCmfzArticle(CmfzArticle cmfzArticle) {
        Map map = new HashMap();
        try {
            cmfzArticleDao.updateCmfzArticle(cmfzArticle);
            map.put("updateOk", true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            map.put("updateOk", false);
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public List<CmfzArticle> showAllArticle() {
        return cmfzArticleDao.showAllArticle();
    }
}
